<template>
  <div>
    <div>
      <div style="display: flex;justify-content: space-between">
        <div>
          <el-input placeholder="请输入员工ID或姓名进行搜索，可以直接回车搜索..." prefix-icon="el-icon-search"
                    clearable
                    @clear="initRecords"
                    style="width: 350px;margin-right: 10px" v-model="searchKeyword"
                    @keydown.enter.native="search"></el-input>
          <el-button icon="el-icon-search" type="primary" @click="search">
            搜索
          </el-button>
        </div>
      </div>
    </div>
    <div style="margin-top: 10px">
      <el-table
          :data="records"
          stripe
          border
          v-loading="loading"
          element-loading-text="正在加载..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          style="width: 100%">
        <el-table-column
            prop="eid"
            label="员工ID"
            width="100">
        </el-table-column>
        <el-table-column
            prop="employeeName"
            label="员工姓名"
            width="100">
        </el-table-column>
        <el-table-column
          prop="departmentName"
          label="员工部门"
          width="100">
        </el-table-column>
        <el-table-column
            prop="phone"
            label="员工电话"
            width="100">
        </el-table-column>
        <el-table-column
            prop="clockTime"
            label="打卡时间"
            width="180">
        </el-table-column>
        <el-table-column
            prop="clockStatus"
            label="打卡状态"
            width="100">
        </el-table-column>
        <el-table-column
            prop="clockType"
            label="打卡类型"
            width="100">
        </el-table-column>
        <el-table-column
            prop="remark"
            label="备注"
            width="200">
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button @click="showEditView(scope.row)" style="padding: 3px" size="mini">编辑</el-button>
            <el-button @click="deleteRecord(scope.row)" style="padding: 3px" size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
        title="编辑打卡记录"
        :visible.sync="dialogVisible"
        width="50%">
      <div>
        <el-form :model="editRecord" :rules="rules" ref="editForm">
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工姓名:" prop="employeeName">
                <el-input size="mini" style="width: 200px" prefix-icon="el-icon-edit" v-model="editRecord.employeeName"
                          placeholder="请输入员工姓名" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="打卡时间:" prop="clockTime">
                <el-date-picker
                    v-model="editRecord.clockTime"
                    size="mini"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    style="width: 200px;"
                    placeholder="打卡时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="打卡类型:" prop="clockType">
                <el-select v-model="editRecord.clockType" placeholder="打卡类型" size="mini" style="width: 200px;">
                  <el-option label="上班" value="上班"></el-option>
                  <el-option label="下班" value="下班"></el-option>
                  <el-option label="休息" value="休息"></el-option>
                  <el-option label="暂离" value="暂离"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="打卡状态:" prop="clockStatus">
                <el-select v-model="editRecord.clockStatus" placeholder="打卡状态" size="mini" style="width: 200px;">
                  <el-option label="正常" value="正常"></el-option>
                  <el-option label="迟到" value="迟到"></el-option>
                  <el-option label="早退" value="早退"></el-option>
                  <el-option label="缺勤" value="缺勤"></el-option>
                  <el-option label="请假" value="请假"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属部门:" prop="departmentName">
                <el-input size="mini" style="width: 200px" prefix-icon="el-icon-edit"
                          v-model="editRecord.departmentName" placeholder="所属部门" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话:" prop="phone">
                <el-input size="mini" style="width: 200px" prefix-icon="el-icon-phone"
                          v-model="editRecord.phone" placeholder="联系电话" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注:" prop="remark">
                <el-input size="mini" style="width: 100%" prefix-icon="el-icon-edit"
                          v-model="editRecord.remark" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRecord">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Attendance",
  data() {
    return {
      records: [],
      loading: false,
      searchKeyword: '',
      dialogVisible: false,
      editRecord: {
        id: null,
        eid: null,
        employeeName: '',
        clockType: "",
        phone: "",
        departmentName: '',
        clockTime: '',
        clockStatus: '',
        remark: ''
      },
      rules: {
        clockTime: [{required: true, message: '请输入打卡时间', trigger: 'blur'}],
        clockStatus: [{required: true, message: '请选择打卡状态', trigger: 'blur'}],
        clockType: [{required: true, message: '请选择打卡类型', trigger: 'blur'}]
      }
    }
  },
  mounted() {
    this.initRecords();
  },
  methods: {
    showEditView(data) {
      this.editRecord = JSON.parse(JSON.stringify(data));
      this.dialogVisible = true;
    },
    updateRecord() {
      console.log(this.editRecord)
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.putRequest("/attendance/record/update", this.editRecord).then(resp => {
            if (resp) {

              this.dialogVisible = false;
              this.initRecords();
              this.$message({
                type: 'success',
                message: '更新成功!'
              });
            }
          })
        }
      });
    },
    deleteRecord(data) {
      this.$confirm('确认删除该打卡记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/attendance/record/delete/" + data.id).then(resp => {
          if (resp) {
            this.initRecords();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    initRecords() {
      this.loading = true;
      this.getRequest("/attendance/records").then(resp => {
        this.loading = false;
        if (resp) {
          this.records = resp;
        }
      })
    },
    search() {
      if (!this.searchKeyword) {
        this.initRecords();
        return;
      }
      this.loading = true;
      const isId = /^\d+$/.test(this.searchKeyword);
      const url = isId ?
          `/attendance/records/id/${this.searchKeyword}` :
          `/attendance/records/name/${this.searchKeyword}`;

      this.getRequest(url).then(resp => {
        this.loading = false;
        if (resp) {
          this.records = resp;
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
